package com.javacv.plus.extensions.video;

import com.javacv.plus.core.config.ProcessorConfig;
import com.javacv.plus.core.processor.MediaProcessor;
import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.extensions.video.config.VideoConfig;
import com.javacv.plus.extensions.video.steps.*;
import lombok.extern.slf4j.Slf4j;

/**
 * 视频处理器
 * 提供视频处理的链式调用API
 */
@Slf4j
public class VideoProcessor extends MediaProcessor<VideoProcessor, VideoResult> {

    protected VideoProcessor(ProcessorConfig config) {
        super(config);
    }

    /**
     * 创建视频处理器
     */
    public static VideoProcessor of(String videoPath) {
        VideoConfig config = new VideoConfig();
        config.setInputPath(videoPath);
        
        VideoProcessor processor = new VideoProcessor(config);
        processor.addStep(new LoadVideoStep());
        return processor;
    }

    /**
     * 创建视频处理器（自定义配置）
     */
    public static VideoProcessor of(String videoPath, VideoConfig config) {
        config.setInputPath(videoPath);
        
        VideoProcessor processor = new VideoProcessor(config);
        processor.addStep(new LoadVideoStep());
        return processor;
    }

    /**
     * 调整视频分辨率
     */
    public VideoProcessor resize(int width, int height) {
        return addStep(new VideoResizeStep(width, height));
    }

    /**
     * 裁剪视频时长
     */
    public VideoProcessor trim(double startSeconds, double durationSeconds) {
        return addStep(new VideoTrimStep(startSeconds, durationSeconds));
    }

    /**
     * 调整视频帧率
     */
    public VideoProcessor frameRate(double fps) {
        return addStep(new FrameRateStep(fps));
    }

    /**
     * 添加视频滤镜
     */
    public VideoProcessor addFilter(VideoFilterStep.FilterType filterType, double intensity) {
        return addStep(new VideoFilterStep(filterType, intensity));
    }

    /**
     * 添加水印
     */
    public VideoProcessor addWatermark(String watermarkPath, int x, int y) {
        return addStep(new VideoWatermarkStep(watermarkPath, x, y));
    }

    /**
     * 提取视频帧
     */
    public VideoProcessor extractFrames(int interval) {
        return addStep(new ExtractFramesStep(interval));
    }

    /**
     * 转换视频格式
     */
    public VideoProcessor convertFormat(String format) {
        getConfig().setProperty("outputFormat", format);
        return this;
    }

    /**
     * 压缩视频
     */
    public VideoProcessor compress(String quality) {
        return addStep(new VideoCompressStep(quality));
    }

    /**
     * 保存视频
     */
    public VideoResult save(String outputPath) {
        getConfig().setOutputPath(outputPath);
        addStep(new SaveVideoStep());
        return process();
    }

    /**
     * 获取处理结果但不保存
     */
    public VideoResult get() {
        return process();
    }

    @Override
    protected VideoResult executeProcess() throws Exception {
        ProcessorContext context = new ProcessorContext();
        
        // 执行处理链
        chain.execute(this, context);
        
        // 创建结果
        VideoResult result = new VideoResult();
        result.setSourcePath(getConfig().getInputPath());
        result.setOutputPath(getConfig().getOutputPath());
        result.setSuccess(true);
        
        return result;
    }

    /**
     * 获取视频配置
     */
    public VideoConfig getVideoConfig() {
        return (VideoConfig) getConfig();
    }
} 